/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.vdialog.view.instance;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import offset.nodes.client.model.Server;
import offset.nodes.client.tree.model.TreeNodePanel;
import offset.nodes.client.tree.model.TreeNodePanelContainer;
import offset.nodes.client.model.TreeNodeUserObject;
import offset.nodes.client.chooser.view.NodeChooserDialog;
import offset.nodes.client.chooser.view.QueryResultChooserDialog;
import offset.nodes.client.view.SimpleDialog;
import offset.nodes.client.vdialog.model.instance.AbstractTemplateNodeInstance;
import offset.nodes.client.vdialog.model.instance.VirtualFolderInstance;

/**
 *
 * @author  Walter Lütgenau
 */
public class VirtualFolderInstancePanel extends javax.swing.JPanel implements TreeNodePanel {

    class TextFieldDocumentListener implements DocumentListener {

        public void changedUpdate(DocumentEvent e) {
            validateOkButton();
        }

        public void insertUpdate(DocumentEvent e) {
            validateOkButton();
        }

        public void removeUpdate(DocumentEvent e) {
            validateOkButton();
        }
    }
    TreeNodePanelContainer container = null;
    Server server;
    TextFieldDocumentListener textFieldDocumentListener = new TextFieldDocumentListener();
    VirtualFolderInstance userObject;

    /** Creates new form FolderPanel
     * @param server the server model
     * @param types the node types repository
     */
    public VirtualFolderInstancePanel(Server server) {
        this.server = server;
        initComponents();
    }

    private void validateOkButton() {
        boolean ok = true;
        if (queryTextField.getText() == null || queryTextField.getText().length() == 0 ) {
            ok = false;
        }

        container.enableOkButton(ok);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        folderNameButtonGroup = new javax.swing.ButtonGroup();
        queryLabel = new javax.swing.JLabel();
        queryTextField = new javax.swing.JTextField();
        queryTextField.getDocument().addDocumentListener(textFieldDocumentListener);
        newButton = new javax.swing.JButton();
        existingButton = new javax.swing.JButton();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("offset/nodes/client/vdialog/view/DialogBundle"); // NOI18N
        queryLabel.setText(bundle.getString("template.templateQueryPanel.query")); // NOI18N

        newButton.setText(bundle.getString("query.anyFolder")); // NOI18N
        newButton.setPreferredSize(new java.awt.Dimension(67, 23));
        newButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectNewFolder(evt);
            }
        });

        existingButton.setText(bundle.getString("query.dataFolder")); // NOI18N
        existingButton.setPreferredSize(new java.awt.Dimension(67, 23));
        existingButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectExistingFolder(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 185, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(existingButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(queryLabel)
                        .addGap(18, 18, 18)
                        .addComponent(queryTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 332, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(45, 45, 45))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(queryLabel)
                    .addComponent(queryTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(existingButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(newButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(189, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    

    
    private void selectNewFolder(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectNewFolder
        try {
            NodeChooserDialog dialog = new NodeChooserDialog(null, true, server, "/", "*");
            dialog.setVisible(true);
            if (dialog.getReturnStatus() == NodeChooserDialog.RET_OK) {
                queryTextField.setText(dialog.getPath());
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
}//GEN-LAST:event_selectNewFolder

    private void selectExistingFolder(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectExistingFolder
        try {
            QueryResultChooserDialog dialog = new QueryResultChooserDialog(null, true, server, "//element(*," + userObject.getNodeType() +")");
            dialog.setVisible(true);
            if (dialog.getResult() == SimpleDialog.OK) {
                queryTextField.setText(dialog.getPath());
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
}//GEN-LAST:event_selectExistingFolder

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton existingButton;
    private javax.swing.ButtonGroup folderNameButtonGroup;
    private javax.swing.JButton newButton;
    private javax.swing.JLabel queryLabel;
    private javax.swing.JTextField queryTextField;
    // End of variables declaration//GEN-END:variables
    public void setPanel(TreeNodeUserObject userObject) {
        if ( userObject == null || !(userObject instanceof AbstractTemplateNodeInstance))
            return;
        this.userObject = (VirtualFolderInstance) userObject;
        
        if ( this.userObject.getQuery() == null || this.userObject.getQuery().length() == 0 ) {
            queryTextField.setEnabled(false);
            newButton.setEnabled(false);
            existingButton.setEnabled(false);
        }
        else {
            queryTextField.setText(this.userObject.getQuery());
            queryTextField.setEnabled(true);
            newButton.setEnabled(true);
            existingButton.setEnabled(true);
        }
    }

    public TreeNodeUserObject getUserObject() {
        userObject.setQuery(queryTextField.getText());
        return userObject;
    }

    public void setContainer(TreeNodePanelContainer container) {
        this.container = container;
    }
}
